<!DOCTYPE html>
<html lang="en-us">
  <head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    
<meta charset="UTF-8">
<title>Percolator type | Elasticsearch Guide [7.7] | Elastic</title>
<link rel="home" href="index.html" title="Elasticsearch Guide [7.7]">
<link rel="up" href="mapping-types.html" title="Field datatypes">
<link rel="prev" href="object.html" title="Object datatype">
<link rel="next" href="range.html" title="Range datatypes">
<meta name="DC.type" content="Learn/Docs/Elasticsearch/Reference/7.7">
<meta name="DC.subject" content="Elasticsearch">
<meta name="DC.identifier" content="7.7">
<meta name="robots" content="noindex,nofollow">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdn.optimizely.com/js/18132920325.js"></script>
    <link rel="apple-touch-icon" sizes="57x57" href="/apple-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="/apple-icon-60x60.png">
    <link rel="apple-touch-icon" sizes="72x72" href="/apple-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="76x76" href="/apple-icon-76x76.png">
    <link rel="apple-touch-icon" sizes="114x114" href="/apple-icon-114x114.png">
    <link rel="apple-touch-icon" sizes="120x120" href="/apple-icon-120x120.png">
    <link rel="apple-touch-icon" sizes="144x144" href="/apple-icon-144x144.png">
    <link rel="apple-touch-icon" sizes="152x152" href="/apple-icon-152x152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="/apple-icon-180x180.png">
    <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">
    <link rel="icon" type="image/png" href="/android-chrome-192x192.png" sizes="192x192">
    <link rel="icon" type="image/png" href="/favicon-96x96.png" sizes="96x96">
    <link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16">
    <link rel="manifest" href="/manifest.json">
    <meta name="apple-mobile-web-app-title" content="Elastic">
    <meta name="application-name" content="Elastic">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="msapplication-TileImage" content="/mstile-144x144.png">
    <meta name="theme-color" content="#ffffff">
    <meta name="naver-site-verification" content="936882c1853b701b3cef3721758d80535413dbfd">
    <meta name="yandex-verification" content="d8a47e95d0972434">
    <meta name="localized" content="true">
    <meta name="st:robots" content="follow,index">
    <meta property="og:image" content="https://www.elastic.co/static/images/elastic-logo-200.png">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" href="/favicon.ico" type="image/x-icon">
    <link rel="apple-touch-icon-precomposed" sizes="64x64" href="/favicon_64x64_16bit.png">
    <link rel="apple-touch-icon-precomposed" sizes="32x32" href="/favicon_32x32.png">
    <link rel="apple-touch-icon-precomposed" sizes="16x16" href="/favicon_16x16.png">
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="/guide/static/styles.css">
  </head>

  <!--© 2015-2021 Elasticsearch B.V. Copying, publishing and/or distributing without written permission is strictly prohibited.-->

  <body>
    <!-- Google Tag Manager -->
    <script>dataLayer = [];</script><noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-58RLH5" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-58RLH5');</script>
    <!-- End Google Tag Manager -->

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-12395217-16"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'UA-12395217-16');
    </script>

    <!--BEGIN QUALTRICS WEBSITE FEEDBACK SNIPPET-->
    <script type="text/javascript">
      (function(){var g=function(e,h,f,g){
      this.get=function(a){for(var a=a+"=",c=document.cookie.split(";"),b=0,e=c.length;b<e;b++){for(var d=c[b];" "==d.charAt(0);)d=d.substring(1,d.length);if(0==d.indexOf(a))return d.substring(a.length,d.length)}return null};
      this.set=function(a,c){var b="",b=new Date;b.setTime(b.getTime()+6048E5);b="; expires="+b.toGMTString();document.cookie=a+"="+c+b+"; path=/; "};
      this.check=function(){var a=this.get(f);if(a)a=a.split(":");else if(100!=e)"v"==h&&(e=Math.random()>=e/100?0:100),a=[h,e,0],this.set(f,a.join(":"));else return!0;var c=a[1];if(100==c)return!0;switch(a[0]){case "v":return!1;case "r":return c=a[2]%Math.floor(100/c),a[2]++,this.set(f,a.join(":")),!c}return!0};
      this.go=function(){if(this.check()){var a=document.createElement("script");a.type="text/javascript";a.src=g;document.body&&document.body.appendChild(a)}};
      this.start=function(){var a=this;window.addEventListener?window.addEventListener("load",function(){a.go()},!1):window.attachEvent&&window.attachEvent("onload",function(){a.go()})}};
      try{(new g(100,"r","QSI_S_ZN_emkP0oSe9Qrn7kF","https://znemkp0ose9qrn7kf-elastic.siteintercept.qualtrics.com/WRSiteInterceptEngine/?Q_ZID=ZN_emkP0oSe9Qrn7kF")).start()}catch(i){}})();
    </script><div id="ZN_emkP0oSe9Qrn7kF"><!--DO NOT REMOVE-CONTENTS PLACED HERE--></div>
    <!--END WEBSITE FEEDBACK SNIPPET-->

    <div id="elastic-nav" style="display:none;"></div>
    <script src="https://www.elastic.co/elastic-nav.js"></script>

    <!-- Subnav -->
    <div>
      <div>
        <div class="tertiary-nav d-none d-md-block">
          <div class="container">
            <div class="p-t-b-15 d-flex justify-content-between nav-container">
              <div class="breadcrum-wrapper"><span><a href="/guide/" style="font-size: 14px; font-weight: 600; color: #000;">Docs</a></span></div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="main-container">
      <section id="content">
        <div class="content-wrapper">

          <section id="guide" lang="en">
            <div class="container">
              <div class="row">
                <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                  <!-- start body -->
                  <div class="page_header">
<strong>IMPORTANT</strong>: No additional bug fixes or documentation updates
will be released for this version. For the latest information, see the
<a href="../current/index.html">current release documentation</a>.
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="mapping.html">Mapping</a></span>
»
<span class="breadcrumb-link"><a href="mapping-types.html">Field datatypes</a></span>
»
<span class="breadcrumb-node">Percolator type</span>
</div>
<div class="navheader">
<span class="prev">
<a href="object.html">« Object datatype</a>
</span>
<span class="next">
<a href="range.html">Range datatypes »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="percolator"></a>Percolator type<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/types/percolator.asciidoc">edit</a>
</h2>
</div></div></div>

<p>The <code class="literal">percolator</code> field type parses a json structure into a native query and
stores that query, so that the <a class="xref" href="query-dsl-percolate-query.html" title="Percolate query">percolate query</a>
can use it to match provided documents.</p>
<p>Any field that contains a json object can be configured to be a percolator
field. The percolator field type has no settings. Just configuring the <code class="literal">percolator</code>
field type is sufficient to instruct Elasticsearch to treat a field as a
query.</p>
<p>If the following mapping configures the <code class="literal">percolator</code> field type for the
<code class="literal">query</code> field:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT my_index
{
    "mappings": {
        "properties": {
            "query": {
                "type": "percolator"
            },
            "field": {
                "type": "text"
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/677.console"></div>
<p>Then you can index a query:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT my_index/_doc/match_value
{
    "query" : {
        "match" : {
            "field" : "value"
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/678.console"></div>
<div class="important admon">
<div class="icon"></div>
<div class="admon_content">
<p>Fields referred to in a percolator query must <span class="strong strong"><strong>already</strong></span> exist in the mapping
associated with the index used for percolation. In order to make sure these fields exist,
add or update a mapping via the <a class="xref" href="indices-create-index.html" title="Create index API">create index</a> or <a class="xref" href="indices-put-mapping.html" title="Put mapping API">put mapping</a> APIs.</p>
</div>
</div>
<h4>
<a id="_reindexing_your_percolator_queries"></a>Reindexing your percolator queries<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/types/percolator.asciidoc">edit</a>
</h4>
<p>Reindexing percolator queries is sometimes required to benefit from improvements made to the <code class="literal">percolator</code> field type in
new releases.</p>
<p>Reindexing percolator queries can be reindexed by using the <a class="xref" href="docs-reindex.html" title="Reindex API">reindex api</a>.
Lets take a look at the following index with a percolator field type:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT index
{
  "mappings": {
    "properties": {
      "query" : {
        "type" : "percolator"
      },
      "body" : {
        "type": "text"
      }
    }
  }
}

POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "index",
        "alias": "queries" <a id="CO310-1"></a><i class="conum" data-value="1"></i>
      }
    }
  ]
}

PUT queries/_doc/1?refresh
{
  "query" : {
    "match" : {
      "body" : "quick brown fox"
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/679.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO310-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>It is always recommended to define an alias for your index, so that in case of a reindex systems / applications
don’t need to be changed to know that the percolator queries are now in a different index.</p>
</td>
</tr>
</table>
</div>
<p>Lets say you’re going to upgrade to a new major version and in order for the new Elasticsearch version to still be able
to read your queries you need to reindex your queries into a new index on the current Elasticsearch version:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT new_index
{
  "mappings": {
    "properties": {
      "query" : {
        "type" : "percolator"
      },
      "body" : {
        "type": "text"
      }
    }
  }
}

POST /_reindex?refresh
{
  "source": {
    "index": "index"
  },
  "dest": {
    "index": "new_index"
  }
}

POST _aliases
{
  "actions": [ <a id="CO311-1"></a><i class="conum" data-value="1"></i>
    {
      "remove": {
        "index" : "index",
        "alias": "queries"
      }
    },
    {
      "add": {
        "index": "new_index",
        "alias": "queries"
      }
    }
  ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/680.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO311-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>If you have an alias don’t forget to point it to the new index.</p>
</td>
</tr>
</table>
</div>
<p>Executing the <code class="literal">percolate</code> query via the <code class="literal">queries</code> alias:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /queries/_search
{
  "query": {
    "percolate" : {
      "field" : "query",
      "document" : {
        "body" : "fox jumps over the lazy dog"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/681.console"></div>
<p>now returns matches from the new index:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped" : 0,
    "failed": 0
  },
  "hits": {
    "total" : {
        "value": 1,
        "relation": "eq"
    },
    "max_score": 0.13076457,
    "hits": [
      {
        "_index": "new_index", <a id="CO312-1"></a><i class="conum" data-value="1"></i>
        "_type": "_doc",
        "_id": "1",
        "_score": 0.13076457,
        "_source": {
          "query": {
            "match": {
              "body": "quick brown fox"
            }
          }
        },
        "fields" : {
          "_percolator_document_slot" : [0]
        }
      }
    ]
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO312-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Percolator query hit is now being presented from the new index.</p>
</td>
</tr>
</table>
</div>
<h4>
<a id="_optimizing_query_time_text_analysis"></a>Optimizing query time text analysis<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/types/percolator.asciidoc">edit</a>
</h4>
<p>When the percolator verifies a percolator candidate match it is going to parse, perform query time text analysis and actually run
the percolator query on the document being percolated. This is done for each candidate match and every time the <code class="literal">percolate</code> query executes.
If your query time text analysis is relatively expensive part of query parsing then text analysis can become the
dominating factor time is being spent on when percolating. This query parsing overhead can become noticeable when the
percolator ends up verifying many candidate percolator query matches.</p>
<p>To avoid the most expensive part of text analysis at percolate time. One can choose to do the expensive part of text analysis
when indexing the percolator query. This requires using two different analyzers. The first analyzer actually performs
text analysis that needs be performed (expensive part). The second analyzer (usually whitespace) just splits the generated tokens
that the first analyzer has produced. Then before indexing a percolator query, the analyze api should be used to analyze the query
text with the more expensive analyzer. The result of the analyze api, the tokens, should be used to substitute the original query
text in the percolator query. It is important that the query should now be configured to override the analyzer from the mapping and
just the second analyzer. Most text based queries support an <code class="literal">analyzer</code> option (<code class="literal">match</code>, <code class="literal">query_string</code>, <code class="literal">simple_query_string</code>).
Using this approach the expensive text analysis is performed once instead of many times.</p>
<p>Lets demonstrate this workflow via a simplified example.</p>
<p>Lets say we want to index the following percolator query:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "query" : {
    "match" : {
      "body" : {
        "query" : "missing bicycles"
      }
    }
  }
}</pre>
</div>
<p>with these settings and mapping:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /test_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer" : {
          "tokenizer": "standard",
          "filter" : ["lowercase", "porter_stem"]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "query" : {
        "type": "percolator"
      },
      "body" : {
        "type": "text",
        "analyzer": "my_analyzer" <a id="CO313-1"></a><i class="conum" data-value="1"></i>
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/682.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO313-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>For the purpose of this example, this analyzer is considered expensive.</p>
</td>
</tr>
</table>
</div>
<p>First we need to use the analyze api to perform the text analysis prior to indexing:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /test_index/_analyze
{
  "analyzer" : "my_analyzer",
  "text" : "missing bicycles"
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/683.console"></div>
<p>This results the following response:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "tokens": [
    {
      "token": "miss",
      "start_offset": 0,
      "end_offset": 7,
      "type": "&lt;ALPHANUM&gt;",
      "position": 0
    },
    {
      "token": "bicycl",
      "start_offset": 8,
      "end_offset": 16,
      "type": "&lt;ALPHANUM&gt;",
      "position": 1
    }
  ]
}</pre>
</div>
<p>All the tokens in the returned order need to replace the query text in the percolator query:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /test_index/_doc/1?refresh
{
  "query" : {
    "match" : {
      "body" : {
        "query" : "miss bicycl",
        "analyzer" : "whitespace" <a id="CO314-1"></a><i class="conum" data-value="1"></i>
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/684.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO314-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>It is important to select a whitespace analyzer here, otherwise the analyzer defined in the mapping will be used,
which defeats the point of using this workflow. Note that <code class="literal">whitespace</code> is a built-in analyzer, if a different analyzer
needs to be used, it needs to be configured first in the index’s settings.</p>
</td>
</tr>
</table>
</div>
<p>The analyze api prior to the indexing the percolator flow should be done for each percolator query.</p>
<p>At percolate time nothing changes and the <code class="literal">percolate</code> query can be defined normally:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /test_index/_search
{
  "query": {
    "percolate" : {
      "field" : "query",
      "document" : {
        "body" : "Bycicles are missing"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/685.console"></div>
<p>This results in a response like this:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped" : 0,
    "failed": 0
  },
  "hits": {
    "total" : {
        "value": 1,
        "relation": "eq"
    },
    "max_score": 0.13076457,
    "hits": [
      {
        "_index": "test_index",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.13076457,
        "_source": {
          "query": {
            "match": {
              "body": {
                "query": "miss bicycl",
                "analyzer": "whitespace"
              }
            }
          }
        },
        "fields" : {
          "_percolator_document_slot" : [0]
        }
      }
    ]
  }
}</pre>
</div>
<h4>
<a id="_optimizing_wildcard_queries"></a>Optimizing wildcard queries.<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/types/percolator.asciidoc">edit</a>
</h4>
<p>Wildcard queries are  more expensive than other queries for the percolator,
especially if the wildcard expressions are large.</p>
<p>In the case of <code class="literal">wildcard</code> queries with prefix wildcard expressions or just the <code class="literal">prefix</code> query,
the <code class="literal">edge_ngram</code> token filter can be used to replace these queries with regular <code class="literal">term</code>
query on a field where the <code class="literal">edge_ngram</code> token filter is configured.</p>
<p>Creating an index with custom analysis settings:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT my_queries1
{
  "settings": {
    "analysis": {
      "analyzer": {
        "wildcard_prefix": { <a id="CO315-1"></a><i class="conum" data-value="1"></i>
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "wildcard_edge_ngram"
          ]
        }
      },
      "filter": {
        "wildcard_edge_ngram": { <a id="CO315-2"></a><i class="conum" data-value="2"></i>
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 32
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "query": {
        "type": "percolator"
      },
      "my_field": {
        "type": "text",
        "fields": {
          "prefix": { <a id="CO315-3"></a><i class="conum" data-value="3"></i>
            "type": "text",
            "analyzer": "wildcard_prefix",
            "search_analyzer": "standard"
          }
        }
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/686.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO315-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>The analyzer that generates the prefix tokens to be used at index time only.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO315-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>Increase the <code class="literal">min_gram</code> and decrease <code class="literal">max_gram</code> settings based on your prefix search needs.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO315-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>This multifield should be used to do the prefix search
with a <code class="literal">term</code> or <code class="literal">match</code> query instead of a <code class="literal">prefix</code> or <code class="literal">wildcard</code> query.</p>
</td>
</tr>
</table>
</div>
<p>Then instead of indexing the following query:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "query": {
    "wildcard": {
      "my_field": "abc*"
    }
  }
}</pre>
</div>
<p>this query below should be indexed:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /my_queries1/_doc/1?refresh
{
  "query": {
    "term": {
      "my_field.prefix": "abc"
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/687.console"></div>
<p>This way can handle the second query more efficiently than the first query.</p>
<p>The following search request will match with the previously indexed
percolator query:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /my_queries1/_search
{
  "query": {
    "percolate": {
      "field": "query",
      "document": {
        "my_field": "abcd"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/688.console"></div>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total" : {
        "value": 1,
        "relation": "eq"
    },
    "max_score": 0.18864399,
    "hits": [
      {
        "_index": "my_queries1",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.18864399,
        "_source": {
          "query": {
            "term": {
              "my_field.prefix": "abc"
            }
          }
        },
        "fields": {
          "_percolator_document_slot": [
            0
          ]
        }
      }
    ]
  }
}</pre>
</div>
<p>The same technique can also be used to speed up suffix
wildcard searches. By using the <code class="literal">reverse</code> token filter
before the <code class="literal">edge_ngram</code> token filter.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT my_queries2
{
  "settings": {
    "analysis": {
      "analyzer": {
        "wildcard_suffix": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "reverse",
            "wildcard_edge_ngram"
          ]
        },
        "wildcard_suffix_search_time": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "reverse"
          ]
        }
      },
      "filter": {
        "wildcard_edge_ngram": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 32
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "query": {
        "type": "percolator"
      },
      "my_field": {
        "type": "text",
        "fields": {
          "suffix": {
            "type": "text",
            "analyzer": "wildcard_suffix",
            "search_analyzer": "wildcard_suffix_search_time" <a id="CO316-1"></a><i class="conum" data-value="1"></i>
          }
        }
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/689.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO316-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>A custom analyzer is needed at search time too, because otherwise
the query terms are not being reversed and would otherwise not match
with the reserved suffix tokens.</p>
</td>
</tr>
</table>
</div>
<p>Then instead of indexing the following query:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "query": {
    "wildcard": {
      "my_field": "*xyz"
    }
  }
}</pre>
</div>
<p>the following query below should be indexed:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /my_queries2/_doc/2?refresh
{
  "query": {
    "match": { <a id="CO317-1"></a><i class="conum" data-value="1"></i>
      "my_field.suffix": "xyz"
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/690.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO317-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>The <code class="literal">match</code> query should be used instead of the <code class="literal">term</code> query,
because text analysis needs to reverse the query terms.</p>
</td>
</tr>
</table>
</div>
<p>The following search request will match with the previously indexed
percolator query:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /my_queries2/_search
{
  "query": {
    "percolate": {
      "field": "query",
      "document": {
        "my_field": "wxyz"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/691.console"></div>
<h4>
<a id="_dedicated_percolator_index"></a>Dedicated Percolator Index<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/types/percolator.asciidoc">edit</a>
</h4>
<p>Percolate queries can be added to any index. Instead of adding percolate queries to the index the data resides in,
these queries can also be added to a dedicated index. The advantage of this is that this dedicated percolator index
can have its own index settings (For example the number of primary and replica shards). If you choose to have a dedicated
percolate index, you need to make sure that the mappings from the normal index are also available on the percolate index.
Otherwise percolate queries can be parsed incorrectly.</p>
<h4>
<a id="_forcing_unmapped_fields_to_be_handled_as_strings"></a>Forcing Unmapped Fields to be Handled as Strings<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/types/percolator.asciidoc">edit</a>
</h4>
<p>In certain cases it is unknown what kind of percolator queries do get registered, and if no field mapping exists for fields
that are referred by percolator queries then adding a percolator query fails. This means the mapping needs to be updated
to have the field with the appropriate settings, and then the percolator query can be added. But sometimes it is sufficient
if all unmapped fields are handled as if these were default text fields. In those cases one can configure the
<code class="literal">index.percolator.map_unmapped_fields_as_text</code> setting to <code class="literal">true</code> (default to <code class="literal">false</code>) and then if a field referred in
a percolator query does not exist, it will be handled as a default text field so that adding the percolator query doesn’t
fail.</p>
<h4>
<a id="_limitations_8"></a>Limitations<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/types/percolator.asciidoc">edit</a>
</h4>
<h5>
<a id="parent-child"></a>Parent/child<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/types/percolator.asciidoc">edit</a>
</h5>
<p>Because the <code class="literal">percolate</code> query is processing one document at a time, it doesn’t support queries and filters that run
against child documents such as <code class="literal">has_child</code> and <code class="literal">has_parent</code>.</p>
<h5>
<a id="_fetching_queries"></a>Fetching queries<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/types/percolator.asciidoc">edit</a>
</h5>
<p>There are a number of queries that fetch data via a get call during query parsing. For example the <code class="literal">terms</code> query when
using terms lookup, <code class="literal">template</code> query when using indexed scripts and <code class="literal">geo_shape</code> when using pre-indexed shapes. When these
queries are indexed by the <code class="literal">percolator</code> field type then the get call is executed once. So each time the <code class="literal">percolator</code>
query evaluates these queries, the fetches terms, shapes etc. as the were upon index time will be used. Important to note
is that fetching of terms that these queries do, happens both each time the percolator query gets indexed on both primary
and replica shards, so the terms that are actually indexed can be different between shard copies, if the source index
changed while indexing.</p>
<h5>
<a id="_script_query"></a>Script query<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/types/percolator.asciidoc">edit</a>
</h5>
<p>The script inside a <code class="literal">script</code> query can only access doc values fields. The <code class="literal">percolate</code> query indexes the provided document
into an in-memory index. This in-memory index doesn’t support stored fields and because of that the <code class="literal">_source</code> field and
other stored fields are not stored. This is the reason why in the <code class="literal">script</code> query the <code class="literal">_source</code> and other stored fields
aren’t available.</p>
<h5>
<a id="_field_aliases"></a>Field aliases<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/mapping/types/percolator.asciidoc">edit</a>
</h5>
<p>Percolator queries that contain <a class="xref" href="alias.html" title="Alias datatype">field aliases</a> may not always behave as expected. In particular, if a
percolator query is registered that contains a field alias, and then that alias is updated in the mappings to refer
to a different field, the stored query will still refer to the original target field. To pick up the change to
the field alias, the percolator query must be explicitly reindexed.</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="object.html">« Object datatype</a>
</span>
<span class="next">
<a href="range.html">Range datatypes »</a>
</span>
</div>
</div>

                  <!-- end body -->
                </div>
                <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                  <div id="rtpcontainer" style="display: block;">
                    <div class="mktg-promo">
                      <h3>Most Popular</h3>
                      <ul class="icons">
                        <li class="icon-elasticsearch-white"><a href="https://www.elastic.co/webinars/getting-started-elasticsearch?baymax=default&amp;elektra=docs&amp;storm=top-video">Get Started with Elasticsearch: Video</a></li>
                        <li class="icon-kibana-white"><a href="https://www.elastic.co/webinars/getting-started-kibana?baymax=default&amp;elektra=docs&amp;storm=top-video">Intro to Kibana: Video</a></li>
                        <li class="icon-logstash-white"><a href="https://www.elastic.co/webinars/introduction-elk-stack?baymax=default&amp;elektra=docs&amp;storm=top-video">ELK for Logs &amp; Metrics: Video</a></li>
                      </ul>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </section>

        </div>


<div id="elastic-footer"></div>
<script src="https://www.elastic.co/elastic-footer.js"></script>
<!-- Footer Section end-->

      </section>
    </div>

<script src="/guide/static/jquery.js"></script>
<script type="text/javascript" src="/guide/static/docs.js"></script>
<script type="text/javascript">
  window.initial_state = {}</script>
  </body>
</html>
